package com.jushu.storbox;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jushu.storbox.callback.IOneboxLastDisTimerListener;
import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.constant.IntentConstant;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.handler.ClientExceptionRelateHandler;
import com.jushu.storbox.newfragment.argument.FileActionArgs;
import com.jushu.storbox.util.OneboxLastDisTimer;
import com.jushu.storbox.util.PublicTools;
import com.jushu.storbox.view.resolve.FilleDisplayResolve;
import com.jushu.storbox.view.resolve.extend.IFileList;
import com.huawei.sharedrive.sdk.android.util.StringUtil;

/**
 */
public class MyFileSearchActivity extends Activity implements IFileList {

	private FilleDisplayResolve fileResolve;
	private EditText search_et;
	private ImageView cleanButton;
	private View mBtnCancel;
	private OneboxLastDisTimer lastDisTimer = new OneboxLastDisTimer(800, 800);
	private ProgressBar progressBar;
	private ImageView searchButton;
	InputMethodManager inputManager;
	Intent intent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.action_search_activity);
		search_et = (EditText) this.findViewById(R.id.share_file_search_edittext);
		search_et.setHint(getResources().getString(R.string.allfile_search_file));
		cleanButton = (ImageView) findViewById(R.id.share_file_clean_imageview);
		mBtnCancel = findViewById(R.id.tv_cancel);
		progressBar = (ProgressBar) findViewById(R.id.share_file_search_ProgressBar);
		searchButton = (ImageView) findViewById(R.id.share_file_search_imageview);
		search_et.addTextChangedListener(new SearchEdittextChangedListener());
		cleanButton.setOnClickListener(searchCleanClickListener);
		mBtnCancel.setOnClickListener(searchCancelClickListener);

		if (null == inputManager) {
			inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		}

		intent = getIntent();
		String ownerId = intent.getStringExtra(IntentConstant.INTENT_DATA_EXTENDS_1);
		String folderName = intent.getStringExtra(IntentConstant.INTENT_DATA_EXTENDS_2);

		FileActionArgs arg = new FileActionArgs();
		arg.setKeyWords("");
		arg.setFolderId(Constant.ROOT_FOLDER_ID);
		arg.setFolderName(folderName);
		arg.setOwnerId(ownerId);

		fileResolve = new FilleDisplayResolve(this, arg) {
			@Override
			protected void doOnRefreshComplete() {
				progressBar.setVisibility(View.GONE);
				searchButton.setVisibility(View.VISIBLE);
				PublicTools.hideSoftInput(inputManager, search_et);
				super.doOnRefreshComplete();
			}

			@Override
			public void openFolder(int index, FileFolderInfo info) {
				// TODO Auto-generated method stub
				if(null == intent) {
					intent = new Intent();
				}
				intent.putExtra(IntentConstant.INTENT_SEARCH_FILE_DATA, info);
				setResult(IntentConstant.INTENT_RESULT_SEARCHFILE_VALUE, intent);
				finish();
			}
		};
		fileResolve.getFolderArgument().setKeyWords("");
		fileResolve.initComponent((PullToRefreshListView) this.findViewById(R.id.file_action_content_list));
		fileResolve.binddingAdapter();
	}

	@Override
	public void updateCurrentFolder(String ownerId, String folderId, String folderName) {
		// write ui logic here
	}

	OnClickListener searchCleanClickListener = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			search_et.setText("");
		}
	};
	OnClickListener searchCancelClickListener = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			if(null == intent){
				intent = new Intent();
			}
			setResult(IntentConstant.INTENT_RESULT_SEARCHFILE_NULL, intent);
			MyFileSearchActivity.this.finish();
		}
	};

	@Override
	public void onBackPressed() {
		if (fileResolve.getFolderStack().isEmpty()) {
			if(null == intent){
				intent = new Intent();
			}
			setResult(IntentConstant.INTENT_RESULT_SEARCHFILE_NULL, intent);
			MyFileSearchActivity.this.finish();
		} else {
			fileResolve.openParentFolder();
		}
	}

	@Override
	public void updateSelectAll(boolean selectState) {
	}

	private class SearchEdittextChangedListener implements TextWatcher {
		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		}

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		}

		@Override
		public void afterTextChanged(final Editable textEdit) {
			String content = search_et.getText().toString().trim();
			if(StringUtil.isBlank(content)){
				progressBar.setVisibility(View.INVISIBLE);
				return;
			}
			IOneboxLastDisTimerListener callback = new IOneboxLastDisTimerListener() {
				@Override
				public void onFinish() {
					String keyword = search_et.getText().toString();
					if(keyword == null || keyword.trim().equals("")){
						return;
					}
					if (textEdit.length() > 0) {
						cleanButton.setVisibility(View.VISIBLE);
						progressBar.setVisibility(View.VISIBLE);
						searchButton.setVisibility(View.GONE);
						fileResolve.getFolderArgument().setKeyWords(search_et.getText().toString());
						fileResolve.getAdapter().cleanListDatasOnly();
						fileResolve.binddingAdapter();
					} else {
						cleanButton.setVisibility(View.GONE);
						progressBar.setVisibility(View.GONE);
						searchButton.setVisibility(View.VISIBLE);
					}
				}
			};
			lastDisTimer.setCallback(callback);
			lastDisTimer.restartTimer();
		}
	}

	ClientExceptionRelateHandler handler = new ClientExceptionRelateHandler() {
  
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
		}

		@Override
		protected void onHandlerBefore(Message msg, String errorCode) {
			progressBar.setVisibility(View.GONE);
			super.onHandlerBefore(msg, errorCode);
		}

		@Override
		public Context getContext() {
			return MyFileSearchActivity.this;
		}

	};
}
